package com.sh.polymedicine.happysale.Interceptor;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by zhouyucheng on 2017/9/27.
 */

public class HeaderInterceptor implements Interceptor {
    private AccessTokenRfresh mAccessTokenRfresh;

    public HeaderInterceptor(AccessTokenRfresh mAccessTokenRfresh) {
        this.mAccessTokenRfresh = mAccessTokenRfresh;
    }

    @Override
    public Response intercept(Chain chain) {
        Response rep = null;
        try {
            Request req = chain.request();
            rep = chain.proceed(req);
            if (null != rep.header("accesstoken")) {
                mAccessTokenRfresh.accessToken(rep.header("accesstoken"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rep;
    }

    public interface AccessTokenRfresh {
        void accessToken(String accessToken);
    }
}
